package o;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import com.sina.weibo.sdk.auth.WbAppInfo;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.json.JSONObject;

public class fnf {
    private static final String ˊ = fnf.class.getName();
    private static fnf ॱ;
    private Context ˏ;

    private fnf(Context context) {
        this.ˏ = context.getApplicationContext();
    }

    public static synchronized fnf ॱ(Context context) {
        fnf o_fnf;
        synchronized (fnf.class) {
            if (ॱ == null) {
                ॱ = new fnf(context);
            }
            o_fnf = ॱ;
        }
        return o_fnf;
    }

    public synchronized WbAppInfo ˋ() {
        return ˏ(this.ˏ);
    }

    public static WbAppInfo ˏ(Context context) {
        return ˊ(context);
    }

    private static WbAppInfo ˊ(Context context) {
        WbAppInfo wbAppInfo = null;
        Intent intent = new Intent("com.sina.weibo.action.sdkidentity");
        intent.addCategory("android.intent.category.DEFAULT");
        List<ResolveInfo> queryIntentServices = context.getPackageManager().queryIntentServices(intent, 0);
        if (queryIntentServices == null || queryIntentServices.isEmpty()) {
            return null;
        }
        for (ResolveInfo resolveInfo : queryIntentServices) {
            if (!(resolveInfo.serviceInfo == null || resolveInfo.serviceInfo.applicationInfo == null || TextUtils.isEmpty(resolveInfo.serviceInfo.packageName))) {
                String str = resolveInfo.serviceInfo.packageName;
                WbAppInfo ॱ = ॱ(context, str);
                if (ॱ == null) {
                    ॱ = wbAppInfo;
                }
                if ("com.sina.weibo".equals(str) || "com.sina.weibog3".equals(str)) {
                    return ॱ;
                }
                wbAppInfo = ॱ;
            }
        }
        return wbAppInfo;
    }

    private static WbAppInfo ॱ(Context context, String str) {
        Exception e;
        Throwable th;
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        InputStream open;
        try {
            byte[] bArr = new byte[4096];
            open = context.createPackageContext(str, 2).getAssets().open("weibo_for_sdk.json");
            try {
                int read;
                Object optString;
                StringBuilder stringBuilder = new StringBuilder();
                while (true) {
                    read = open.read(bArr, 0, 4096);
                    if (read == -1) {
                        break;
                    }
                    stringBuilder.append(new String(bArr, 0, read));
                }
                JSONObject jSONObject;
                if (TextUtils.isEmpty(stringBuilder.toString()) || !fnd.ॱ(context, str)) {
                    jSONObject = new JSONObject(stringBuilder.toString());
                    read = jSONObject.optInt("support_api", -1);
                    optString = jSONObject.optString("authActivityName", null);
                } else {
                    jSONObject = new JSONObject(stringBuilder.toString());
                    read = jSONObject.optInt("support_api", -1);
                    optString = jSONObject.optString("authActivityName", null);
                }
                if (read != -1 && !TextUtils.isEmpty(optString)) {
                    WbAppInfo wbAppInfo = new WbAppInfo();
                    wbAppInfo.setPackageName(str);
                    wbAppInfo.setSupportVersion(read);
                    wbAppInfo.setAuthActivityName(optString);
                    if (open != null) {
                        try {
                            open.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                    return wbAppInfo;
                } else if (open == null) {
                    return null;
                } else {
                    try {
                        open.close();
                        return null;
                    } catch (IOException e3) {
                        e3.printStackTrace();
                        return null;
                    }
                }
            } catch (Exception e4) {
                e = e4;
                try {
                    fnm.ˎ(ˊ, e.getMessage());
                    if (open != null) {
                        return null;
                    }
                    try {
                        open.close();
                        return null;
                    } catch (IOException e32) {
                        e32.printStackTrace();
                        return null;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (open != null) {
                        try {
                            open.close();
                        } catch (IOException e322) {
                            e322.printStackTrace();
                        }
                    }
                    throw th;
                }
            }
        } catch (Exception e5) {
            e = e5;
            open = null;
            fnm.ˎ(ˊ, e.getMessage());
            if (open != null) {
                return null;
            }
            open.close();
            return null;
        } catch (Throwable th3) {
            open = null;
            th = th3;
            if (open != null) {
                open.close();
            }
            throw th;
        }
    }
}
